'''
Created on 03/04/2009

@author: eh01
'''
from yarra.datastore import dsconstants, dsofactory
from yarra.datastore.dso import DSO
from yarra.datastore.dsstub import DSStub
from yarra.datastore.dst import DST
from yarra.event import eventconstants
from yarra.event.event import Event
from yarra.event.eventhandler import BaseEventHandler
from yarra.event.eventmanager import EventManager
from yarra.handler import messagetypeconstants
import datetime
import httplib
import os


class SPEARA2AFaultWebServiceResponseHandler(BaseEventHandler):
    '''
    classdocs
    '''


    def __init__(self):
        '''
        Constructor
        '''
        
    def do(self, data):
        dso = data
        request_id = dso.get_dst(subject=dsconstants.SUBJECT_ROOT,
                    predicate=dsconstants.PREDICATE_REQUEST_ID).get_literal()
        destination = dso.get_dst(subject=dsconstants.SUBJECT_ROOT,
                    predicate=dsconstants.PREDICATE_MESSAGE_REQUEST_NODE).get_literal()
        error_code = dso.get_dst(subject=dsconstants.SUBJECT_ROOT,
                    predicate=dsconstants.PREDICATE_ERROR_CODE).get_literal()
        error_message = dso.get_dst(subject=dsconstants.SUBJECT_ROOT,
                    predicate=dsconstants.PREDICATE_ERROR_MESSAGE).get_literal()
        dst1 = DST(subject=dsconstants.SUBJECT_ROOT, 
                  predicate=dsconstants.PREDICATE_REQUEST_ID, 
                  literal=request_id)                                        
        dst2 = DST(subject=dsconstants.SUBJECT_ROOT, 
                  predicate=dsconstants.PREDICATE_MESSAGE_TYPE, 
                  literal=messagetypeconstants.SPEAR_A2A_FAULT_RESPONSE_MESSAGE_TYPE)
        dst3 = DST(subject=dsconstants.SUBJECT_ROOT, 
                  predicate=dsconstants.PREDICATE_MESSAGE_DESTINATION_NODE, 
                  literal=destination)
        dst4 = DST(subject=dsconstants.SUBJECT_ROOT, 
                  predicate=dsconstants.PREDICATE_MESSAGE_CONTENT, 
                  object=dsconstants.SUBJECT_MESSAGE_CONTENT)
        dst5 = DST(subject=dsconstants.SUBJECT_MESSAGE_CONTENT, 
                  predicate=dsconstants.PREDICATE_ERROR_CODE, 
                  literal=error_code)
        dst6 = DST(subject=dsconstants.SUBJECT_MESSAGE_CONTENT, 
                  predicate=dsconstants.PREDICATE_ERROR_MESSAGE, 
                  literal=error_message)
        new_dso = DSO([dst1, dst2, dst3, dst4, dst5, dst6])
        event = Event(eventconstants.SEND_TO_HUB_EVENT_TYPE, new_dso)
        EventManager().add_event(event)  

class SPEARA2AFaultHTTPResponseHandler(BaseEventHandler):
    def do(self, data):
        dso = data
        request_id = dso.get_dst(subject=dsconstants.SUBJECT_ROOT, 
                                 predicate=dsconstants.PREDICATE_REQUEST_ID).get_literal()
        error_code = int(dso.get_dst(subject=dsconstants.SUBJECT_MESSAGE_CONTENT, 
                                 predicate=dsconstants.PREDICATE_ERROR_CODE).get_literal())
        error_message = dso.get_dst(subject=dsconstants.SUBJECT_MESSAGE_CONTENT, 
                                 predicate=dsconstants.PREDICATE_ERROR_MESSAGE).get_literal()                                                                                                   
        ds = DSStub()
        (http_request, lock) = ds.get_outstanding_requests(int(request_id))
        fault_message = None        
        if error_code == httplib.INTERNAL_SERVER_ERROR:
            fault_message = "spear-not-available"
        else:
            fault_message = "unknown-spear-error"

        relative_path = "../../../pages/im/imerror.html"
        
        # get the html template
        template_path = os.path.join(os.path.dirname(__file__), relative_path)
        file = open(template_path)
        html_template_string = file.read()
        html_string = html_template_string.replace('${fault_message}', fault_message)
        new_dso = dsofactory.create_dso_for_im_http_response_handler(httplib.OK, 
                                                               'text/html', 
                                                               html_string, 
                                                               http_request, 
                                                               lock)
        event = Event(eventconstants.HTTP_RESPONSE_EVENT_TYPE, new_dso)
        EventManager().add_event(event)                